package com.hmall.item.instener;

import cn.hutool.json.JSONUtil;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.impl.ItemService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description SpringRabbitListener
 * @Author zhengyuan&junjun
 * @Date 2024-11-15
 */


/**
 *
 * 上下架商品，需要及时更新缓存中的热销商品列表，建议使用RabbitMQ异步处理
 *
 * **/

@Component
public class SpringRabbitListener {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ItemService itemService;

    @RabbitListener(queues = "top20.queue")
    public  void  simpleListener(String msg){
        System.out.println("简单模型消费者，消费数据："+msg);
        //更新redis热销数据
        String top20key = "Top20";
        List<Item> items = itemService.getTop20();
        redisTemplate.opsForValue().set(top20key, JSONUtil.toJsonStr(items));
        System.out.println("更新热销数据成功");
    }



}
